﻿///// <reference path="../../Scripts/jquery-1.4.2.min.js" />
function $createCallBack(method, context) {
	return function () {
		var l = arguments.length;
		if (l > 0) {
			var args = [];
			for (var i = 0; i < l; i++) {
				args[i] = arguments[i];
			}
			args[l] = context;
			return method.apply(this, args);
		}
		return method.call(this, context);
	}
}

jQuery.fn.extend
({
	removeCss: function (cssName) {
		return this.each(function () {
			var curDom = $(this);
			jQuery.grep(cssName.split(','), function (cssToBeRemoved) {
				curDom.css(cssToBeRemoved, '');
			});
			return curDom;
		});
	}
});

function commonTROver(trID) {
	$("#" + trID).css({ 'background-color': '#FFFFCC' });
}

function commonTROut(trID, originalCSS) {
	$("#" + trID).removeCss('background-color');
	$("#" + trID).addClass(originalCSS);
}

$(document).ready(function () {
	var table = $('.altTable')
	altTable(table);
});

function altTable(table) {
	$(table).addClass("altTable");
	var tbody = $('tbody', table);
	$('tr:even', tbody).css('background-color', '#EBF3FE');
	$('tr:odd', tbody).css('background-color', '#FFFFFF');
	$('tr', tbody).mouseover(function () {
		var tr = $(this);
		var c = tr.css('background-color');
		if (c != '#ffffcc') {
			tr.attr('originalColor', c).css('background-color', '#FFFFCC');
		}
	}).mouseleave(function () {
		var tr = $(this);
		tr.css('background-color', tr.attr('originalColor'));
	});
}

//created by rock. for special alternating table - Category
function altLevelTable(table) {
	table = $(table).width("100%");
	$('thead tr', table).css('color', '#999999');
	var trs = $("tbody tr", table).css({
		'height': '30px',
		'border-bottom': '1px solid #CCCCCC'
	});
	$("tbody tr[title='levelOne']:even", table).css('background-color', '#EBF3FE');
	$("tbody tr[title='levelOne']:odd", table).css('background-color', '#FFFFFF');
	trs.mouseover(function () {
		var tr = $(this);
		tr.attr('orginalColor', tr.css('background-color'))
            .css('background-color', '#FFFFCC');
	}).mouseleave(function () {
		var tr = $(this);
		tr.css('background-color', tr.attr('orginalColor'));
	});
}

/////////////////////////////////处理html控件////////////////////////////////

//get the selected option's text or value of a select element.
function $sel(select, isText) {
	var index = select.selectedIndex;
	if (index >= 0) {
		var item = select[select.selectedIndex];
		return isText ? item.text : item.value;
	}
	else {
		return null;
	}
}

//返回一个select选中的数字。
function $selInt(select) {
	var result = parseInt($sel(select), 10);
	if (isNaN(result)) {
		result = 0;
	}
	return result;
}

function $selectValue(select, value) {
	for (var i = 0; i < select.options.length; i++) {
		var o = select.options[i];
		if (o.value == value) {
			select.selectedIndex = i;
			break;
		}
	}
}

//绑定一个select元素
//select:       select元素
//dataSource:   一个{value:"",text:""}的数组。
function $bindSelect(select, dataSource) {
	var options = select.options;
	while (options.length > 0) {
		options.remove(0);
	}
	for (var i = 0, l = dataSource.length; i < l; i++) {
		var item = dataSource[i];
		var o = document.createElement("option");
		o.value = item.value;
		o.innerHTML = item.text;
		options.appendChild(o);
	}
	if (dataSource.length > 0) {
		select.selectedIndex = 0;
	}
}

function getEmptySelect(id) {
	return "<select id='" + id + "'><option value='0'>--请选择--</option></select>";
}

//find out all checkbox in the parent node.
//and call the callback function to deal with each item.
function dealCbsIn(parent, callBack) {
	var inputs = parent.getElementsByTagName("input");
	for (var i = 0, l = inputs.length; i < l; i++) {
		var item = inputs[i];
		if (item.type == "checkbox") {
			callBack(item);
		}
	}
}

//Make the absoluteDiv to the specific corner of the window.
//absoluteDiv:      a absolute div whose parent is the body object.
//whichCorner:      "leftTop", "rightTop", "leftBottom", "rightBottom", "center".
function relocate(absoluteDiv, whichCorner) {
	var d = document.documentElement;
	var cl = d.scrollLeft;
	var ct = d.scrollTop;
	var cw = d.clientWidth;
	var ch = d.clientHeight;
	var w = absoluteDiv.offsetWidth;
	var h = absoluteDiv.offsetHeight;
	var s = absoluteDiv.style;
	switch (whichCorner) {
		case "leftTop":
			s.left = cl + "px";
			s.top = ct + "px";
			break;
		case "rightTop":
			s.left = cl + cw - w + "px";
			s.top = ct + "px";
			break;
		case "leftBottom":
			s.left = cl + "px";
			s.top = ct + ch - h + "px";
			break;
		case "rightBottom":
			s.left = cl + cw - w + "px";
			s.top = ct + ch - h + "px";
			break;
		case "center":
			s.left = cl + (cw - w) / 2 + "px";
			s.top = ct + (ch - h) / 2 + "px";
			break;
	}
}